//
//  EventDispatcher
//
//  Created by Baluta Cristian on 2008-05-28.
//  Copyright (c) 2008 ralcr.com. All rights reserved.
//
package com.asual.swfaddress;

class EventDispatcher {
	var _events : List<Array<Dynamic>>; // list of arrays with [type of event, and function to call]
	
	public function new () {
		_events = new List<Array<Dynamic>>();
	}
	
	//
	public function addEventListener (type:String, func:Dynamic) :Void {
		_events.add ([type, func]);
	}
	public function removeEventListener (type:String, func:Dynamic) :Void {
		for (event in _events)
			if (event[0] == type && Reflect.compareMethods (event[1], func))
				_events.remove (event);
	}
	public function hasEventListener (type:String) :Bool {
		for (event in _events) {
			if (event[0] == type) return true;
		}
		return false;
	}
	
	/**
	 *	Call functions that have this type of event
	 */
	public function dispatchEvent (e:SWFAddressEvent) :Void {
		for (event in _events)
			if (event[0] == e._type)
				try {
					event[1](e);
				}
				catch (e:Dynamic) {
					trace("[EventDispatcher error {"+event[0]+" -> "+event[1]+"}]");
				}
	}
}
